'''
Author: kun 56216004@qq.com
Date: 2023-02-18 20:34:04
LastEditors: kun 56216004@qq.com
LastEditTime: 2024-08-09 11:43:27
FilePath: \ChatWaifu-main\server\flask_gpt.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from flask import Flask,render_template,request, make_response
app = Flask(__name__)
# from jinja2 import Markup
import json
import requests
from requests.structures import CaseInsensitiveDict
import datetime
import time





@app.route('/')
def hello_world():
    return 'Hello World!'






from ht_sql import *
@app.route('/sql' , methods=['GET'])
def sql():
    data = request_parse(request)
    # 假设有如下 URL
    # http://10.8.54.48:5000/sql?q=john
    q = data.get("q")
    #dict_to_object,以方便访问属性
    # data=dict_to_object(data)
    res=get_res(q)
    return res

def request_parse(req_data):
    '''解析请求数据并以json形式返回'''
    if req_data.method == 'POST':
        data = req_data.json
    elif req_data.method == 'GET':
        data = req_data.args
    return data

class Dict(dict):
    # # self.属性写入 等价于调用dict.__setitem__
    __setattr__ = dict.__setitem__
    # # self.属性读取 等价于调用dict.__setitem__
    __getattribute__ = dict.__getitem__
 
    # # 等价于__setattr__ = dict.__setitem__
    # def __setattr__(self, key, value):
    #     dict.__setitem__(self, key, value)
    # 
    # # 等价于__getattribute__ = dict.__getitem__ 或 __getattr__ = dict.__getitem__
    # def __getattribute__(self, item):
    #     return dict.__getitem__(self, item)

# 递归把dict转换成obj对象【兼容obj.属性和obj[属性]】
def dict_to_object(dictObj):
    if not isinstance(dictObj, dict):
        return dictObj
    inst = Dict()
    for k, v in dictObj.items():
        inst[k] = dict_to_object(v)
    return inst





 


if __name__ == '__main__':
    # app.run()#默认仅本机访问，5000端口  
    # 修改jinja的转义符
    # app.jinja_env.variable_start_string = '[['
    # app.jinja_env.variable_end_string = ']]'

    app.run(host='0.0.0.0',port=8011)#允许所有人访问